using System;
using MySql.Data.MySqlClient;
using System.Data;

namespace ChatServer
{
	public class db
	{
		public static String SQLConnection = "";
		MySqlConnection cn;
		public db()
		{
			Configure cf = new Configure("config");
			SQLConnection = cf.GetTag("sql_connection");
			cn = new MySqlConnection(SQLConnection);
			try
			{
				cn.Open ();
			}
			catch
			{
				throw new Exception("Kết nối cơ sở dữ liệu thất bại, xin kiểm tra lại máy chủ !");
			}
		}
		public Object ExcuteScalar(Object []par)
		{
			MySqlCommand cm = new MySqlCommand(par[0].ToString(),cn);
			for (int i =1;i< par.Length ;i++){
				MySqlParameter sqlpr = new MySqlParameter("@" + i.ToString(),par[i]);
				cm.Parameters.Add(sqlpr);
			}
			return cm.ExecuteScalar();
		}
		public void Excute(Object []par)
		{
			MySqlCommand cm = new MySqlCommand(par[0].ToString(),cn);
			for (int i =1;i< par.Length ;i++){
				MySqlParameter sqlpr = new MySqlParameter("@" + i.ToString(),par[i]);
				cm.Parameters.Add(sqlpr);
			}
			cm.ExecuteNonQuery();
		}
        public DataTable ReadTable(Object []par)
        {
            MySqlDataAdapter da = new MySqlDataAdapter(par[0].ToString(), cn);
            MySqlCommand cm = new MySqlCommand(par[0].ToString(), cn);
            for (int i = 1; i < par.Length; i++)
            {
                MySqlParameter sqlpr = new MySqlParameter("?" + i.ToString(), par[i]);
                cm.Parameters.Add(sqlpr);
            }
            da.SelectCommand = cm;
            DataSet dt = new DataSet();
            da.Fill(dt);
            return (dt.Tables[0]);
        }

		public void close()
		{
			try{
				cn.Close();
			}catch
			{
			}
		}




	}
}

